<?php
/**
 * Created by JetBrains PhpStorm.
 * User: porterzhang
 * Date: 13-3-5 下午4:44
 * To change this template use File | Settings | File Templates.
 */
if (!defined('IN_MOBILE_API')) {
    exit('Access Denied');
}
include_once 'forum.php';
class cloud_api {
    function common() { }

    function output() {
        $variable = array();
        //判断是否缺少参数
        if (empty($_GET['caid']) || empty($_GET['fids'])) {
            $variable = array(
                'code' => 1001,
                'messagestr' => lang('plugin/cloud', '1001'),
            );
            cloud_core::result(cloud_core::variable($variable));
        }

        $fid_array = explode(",", $_GET['fids']);
        $add_fid_array=array();
        foreach ($fid_array as $key => $value) {
            //判断这个应用功能是否存在，如果存在则不添加
            $vluseObj = C::t('#cloud#cloud_app_function')->app_function_by_pid($value,$_GET['caid']);

            if (empty($vluseObj)) {
                //将添加的新功能信息push到一个array中
                array_push($add_fid_array,$value);

                $sysfunction = C::t('#cloud#cloud_function')->fetch_function_info($value); //得到功能信息
                if ($sysfunction['pid'] > 0) { //判断这个功能是否有父id
                    $vluseparent = C::t('#cloud#cloud_app_function')->app_function_by_pid($sysfunction['pid'],$_GET['caid']);
                    if (empty($vluseparent)) {

                        array_push($add_fid_array,$sysfunction['pid']);

                        $data = array(
                            'fid' => $sysfunction['pid'],
                            'caid' => $_GET['caid'],
                            'createtime' => TIMESTAMP,
                        );
                        C::t('#cloud#cloud_app_function')->insert($data);
                    }
                }
                $data = array(
                    'fid' => $value,
                    'caid' => $_GET['caid'],
                    'createtime' => TIMESTAMP,
                );
                C::t('#cloud#cloud_app_function')->insert($data);
            }
        }

        //添加的功能赋给应用管理员，判断此功能是应用功能还是普通功能，
        //应用功能没有地区id，普通功能要分地区添加
    //查询出某个应用系统创建的角色组
        $app_role_sys= C::t('#cloud#cloud_app_role')->app_role_by_app_find($_GET['caid']);

        //得到这个应用下所有有效的地区
        $area_array = C::t('#cloud#cloud_app_district')->fetch_status_by_app($_GET['caid']);

        foreach($add_fid_array as $key=>$value){
            $addappf = C::t('#cloud#cloud_app_function')->app_function_fpid_info($value,$_GET['caid']);
                if($addappf['ftype']==1){
                    $data=array(
                        'rid'=>$app_role_sys['id'],
                        'fid'=>$addappf['fid'],
                        'fpid'=>$addappf['fpid'],
                        'name'=>$addappf['name'],
                        'linkurl'=>$addappf['linkurl'],
                        'icon'=>$addappf['icon'],
                        'sort'=>$addappf['sort'],
                    );
                    C::t('#cloud#cloud_role_purview')->insert($data);
                }else  if($addappf['ftype']==2){
                    foreach($area_array as $keyarea=>$valarea){
                        $data=array(
                            'rid'=>$app_role_sys['id'],
                            'fid'=>$addappf['fid'],
                            'fpid'=>$addappf['fpid'],
                            'name'=>$addappf['name'],
                            'linkurl'=>$addappf['linkurl'],
                            'icon'=>$addappf['icon'],
                            'sort'=>$addappf['sort'],
                            'cadid'=>$valarea['cadid'],
                        );
                        C::t('#cloud#cloud_role_purview')->insert($data);
                    }
                }


        }

        //正常返回
        $variable = array(
            'messagestr' => lang('plugin/cloud', '22000'),
        );
        cloud_core::result(cloud_core::variable($variable));
    }
}

?>

